package com.acquisition.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @description: 用于获取Spring容器中的Bean给普通类使用
 */
@Component("applicationContext")
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(this.applicationContext==null){
            this.applicationContext = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * @Title: getBean
     * @Description: 根据类信息获取实例
     * @param clazz
     * @return
     * @author author
     * @date 2021-01-04 11:47:04
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext==null?null:applicationContext.getBean(clazz);
    }
}